<?php

    /**
     * GenenicAction
     *
     * Generic action selector
     *
	 * main syntax: "module~action@requestType". module should be a valid module name or # (# = get the module of the current request).
	 * Action should be an action name (controller:method). All part are optional, but it should have one part at least.
     *
     * @package      Core
     * @subpackage   Selector
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
	class Core_Selector_GenericAction extends Core_Selector_Action
	{

	////////////////////////////////////////////////////////////////////////////
	//                                 Methods                                //
	////////////////////////////////////////////////////////////////////////////

	    /**
		 * Constructor
		 *
         * @param       string      $selector : the selector
         * @param       boolean     $enableRequestPart : true if the selector can contain the request part
		 * @throws		Core_Exception
		 * @throws		Core_ArgumentNullException
         */
        public function __construct($selector, $enableRequestPart = false)
        {
            if ($selector === null) {
				throw new Core_ArgumentNullException('selector is a null reference');
			}

			$coordinator     = Core_Controller_Coordinator::getInstance();
            $this->_registry = Zend_Registry::getInstance();

            if (preg_match("/^(?:([a-zA-Z0-9_\.]+|\#)~)?([a-zA-Z0-9_:]+|\#)?(?:@([a-zA-Z0-9_]+))?$/", $selector, $m)) {
                $m = array_pad($m, 4, '');

                if (!empty($m[1])) {
                    if ($m[1] == '#') {
                        $this->_module = $coordinator->getModule();
                    } else {
                        $this->_module = $m[1];
                    }
                } else {
                    $this->_module = Core_Context::get();
                }

                if ($m[2] == '#') {
                    $this->_resource = $coordinator->getAction();
                } else {
                    $this->_resource = $m[2];
                }

                $r = explode(':', $this->_resource);

                if (count($r) == 1) {
					$this->_controller = 'Index';
					$this->_method = (empty($r[0])) ? 'index' : $r[0];
                } else {
					$this->_method = (empty($r[1])) ? 'index' : $r[1];
					$this->_controller = (empty($r[0])) ? 'Index' : ucfirst(strtolower($r[0]));
                }

                $this->_resource = $this->_controller .':'. $this->_method;

                if ($m[3] != '' && $enableRequestPart) {
                    $this->_requestType = $m[3];
                } else {
					$request = $coordinator->getRequest();

					if ($request instanceof Core_Controller_Request_Abstract) {
						$this->_requestType = $request->getType();
					} else {
						throw new Core_Exception('Fatal error: request isn\'t an instance of Core_Controller_Request_Abstract');
					}
                }

                $this->_createPath();
            } else {
                throw new Core_Exception('Invalid generic action selector syntax ('. $selector .')');
            }
        }
	}
